package date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @auther XuGeGe
 * @date 2022/6/27
 */
public class Week {


    public static void main(String[] args) throws ParseException {
        int account = 3;
        System.out.println("总次数： " + account);
        int frequencyTime = 6;
        System.out.println("每周：" + frequencyTime);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf1.parse("2022-06-23");
        //计算下一次访问时间
        Date startTime = parse;
        System.out.println("startTime: " + sdf1.format(startTime));
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        //一周 第一天是否为星期天
        boolean isFirstSunday = (cal.getFirstDayOfWeek() == Calendar.SUNDAY);
        //获取开始时间是周几
        int currentWeek = cal.get(Calendar.DAY_OF_WEEK);
        //若一周第一天为星期天，则-1
        if (isFirstSunday) {
            currentWeek = currentWeek - 1;
            if (currentWeek == 0) {
                currentWeek = 7;
            }
        }
        System.out.println("开始日期星期:" + currentWeek);

        //下一次随访时间
        Date nextFollowDate = new Date();
        //结束时间
        Date endDate = new Date();

        if (currentWeek <= frequencyTime) {
            nextFollowDate = getAddDay(startTime, frequencyTime - currentWeek);
        }
        if (currentWeek > frequencyTime) {

            nextFollowDate = getAddDay(startTime, 7 - currentWeek + frequencyTime);
        }
        System.out.println("下一次随访时间: " + sdf1.format(nextFollowDate));

        //计算结束时间
        endDate = getAddDay(nextFollowDate, 7 * (account - 1) + 1);
        System.out.println("结束时间： " + sdf1.format(endDate));

    }


    //遍历数组
    public static int printArray(int[] array, int value) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == value) {
                return i;
            }
        }
        return -1;//当if条件不成立时，默认返回一个负数值-1
    }

    /**
     * 获取当前日期月份最大天数
     */
    public static int gteMaxMonthNumber(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //获取当前日期月份最大天数
        int lastNumber = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return lastNumber;
    }

    //当前日期 + n 天
    public static Date getAddDay(Date startTime, int dayNumber) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(calendar.DATE, dayNumber); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }

    //增加月份
    public static Date addMonth(Date startTime, int month) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(calendar.MONTH, month); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }


}
